

/**
 * 路由白名单
 */
const whiteList = [
	"/pages/login/login",
	"/pages/login/register"
]

export default async function () {
	const list = ['navigateTo', 'redirectTo', 'reLaunch', 'switchTab']
	list.forEach(item => {
		uni.addInterceptor(item, {
			invoke (e) {
				const url = e.url.split('?')[0]
				let pass
				if (whiteList) {
					pass = whiteList.some((item) => {
						if (typeof (item) === 'object' && item.pattern) {
							return item.pattern.test(url)
						}
						return url === item
					})
				}
				if (!pass) {

					//获取应用页面栈
					let pagearr = getCurrentPages();
					//获取当前页面信息
					let currentPage = pagearr[pagearr.length - 1];
					//获取页面传递的信息
					let option =  currentPage.options

					uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000,
						complete: () => {
							uni.navigateTo({
								url: "/pages/login/login"
							})
						}
					})
					return false
				}
				return e
			},
			fail (err) {
				console.log(err)
			}
		})
	})
}
